const Database = require('better-sqlite3');
const path = require('path');

const dbPath = path.join(__dirname, 'data', 'delivery-management.db');
const db = new Database(dbPath, { readonly: true });

console.log('=== PARAMETER DATA INVESTIGATION ===\n');

// 1. Check main instance table data
console.log('1. Main Instance Table Data:');
const instanceData = db.prepare('SELECT * FROM device_instances_piping_cooling_water_spray_system').all();
instanceData.forEach(row => {
  console.log(`  Instance ID: ${row.id}, Object Number: ${row.object_number}`);
  console.log(`  Naming Parameters (should be empty/null):`);
  console.log(`    naming_设计物资编码: ${row.naming_设计物资编码 || 'NULL'}`);
  console.log(`    naming_采购物资编码: ${row.naming_采购物资编码 || 'NULL'}`);
  console.log(`    naming_采购包号批次: ${row.naming_采购包号批次 || 'NULL'}`);
  console.log(`  Position Parameters (should be empty/null):`);
  console.log(`    position_设计设备位号_管线号: ${row.position_设计设备位号_管线号 || 'NULL'}`);
  console.log(`    position_工厂地址: ${row.position_工厂地址 || 'NULL'}`);
  console.log(`  Base Parameters (should be empty/null):`);
  console.log(`    base_params_规格: ${row.base_params_规格 || 'NULL'}`);
  console.log(`    base_params_型号: ${row.base_params_型号 || 'NULL'}`);
  console.log(`    base_params_制造厂家: ${row.base_params_制造厂家 || 'NULL'}`);
});

// 2. Check separate parameter tables with matching instance_id
console.log('\n2. Separate Parameter Tables Data:');

// Check if instance_id = 1 exists in parameter tables
const instanceId = 1; // The ID from the main table

console.log(`\n  Checking parameter tables for instance_id = ${instanceId}:`);

// Naming parameters (table_c2)
const namingParams = db.prepare('SELECT * FROM instance_params_1_table_c2 WHERE instance_id = ?').all(instanceId);
console.log(`    instance_params_1_table_c2: ${namingParams.length} records`);
namingParams.forEach(row => {
  console.log(`      设计物资编码: ${row.设计物资编码 || 'NULL'}`);
  console.log(`      采购物资编码: ${row.采购物资编码 || 'NULL'}`);
  console.log(`      采购包号批次: ${row.采购包号批次 || 'NULL'}`);
});

// Position parameters (table_c6)
const positionParams = db.prepare('SELECT * FROM instance_params_1_table_c6 WHERE instance_id = ?').all(instanceId);
console.log(`    instance_params_1_table_c6: ${positionParams.length} records`);
positionParams.forEach(row => {
  console.log(`      设计设备位号/管线号: ${row['设计设备位号/管线号'] || 'NULL'}`);
  console.log(`      工厂地址: ${row.工厂地址 || 'NULL'}`);
  console.log(`      工厂坐标: ${row.工厂坐标 || 'NULL'}`);
});

// Base parameters (table_c494)
const baseParams = db.prepare('SELECT * FROM instance_params_1_table_c494 WHERE instance_id = ?').all(instanceId);
console.log(`    instance_params_1_table_c494: ${baseParams.length} records`);
baseParams.forEach(row => {
  console.log(`      规格: ${row.规格 || 'NULL'}`);
  console.log(`      型号: ${row.型号 || 'NULL'}`);
  console.log(`      制造厂家: ${row.制造厂家 || 'NULL'}`);
  console.log(`      供货厂家: ${row.供货厂家 || 'NULL'}`);
  console.log(`      设备名称: ${row.设备名称 || 'NULL'}`);
});

// 3. Check if there's any data in the parameter tables at all
console.log('\n3. All Parameter Table Data:');
const allNaming = db.prepare('SELECT * FROM instance_params_1_table_c2').all();
const allPosition = db.prepare('SELECT * FROM instance_params_1_table_c6').all();
const allBase = db.prepare('SELECT * FROM instance_params_1_table_c494').all();

console.log(`  instance_params_1_table_c2: ${allNaming.length} total records`);
allNaming.forEach((row, index) => {
  console.log(`    Record ${index + 1}: instance_id=${row.instance_id}, 设计物资编码=${row.设计物资编码 || 'NULL'}`);
});

console.log(`  instance_params_1_table_c6: ${allPosition.length} total records`);
allPosition.forEach((row, index) => {
  console.log(`    Record ${index + 1}: instance_id=${row.instance_id}, 设计设备位号/管线号=${row['设计设备位号/管线号'] || 'NULL'}`);
});

console.log(`  instance_params_1_table_c494: ${allBase.length} total records`);
allBase.forEach((row, index) => {
  console.log(`    Record ${index + 1}: instance_id=${row.instance_id}, 规格=${row.规格 || 'NULL'}, 设备名称=${row.设备名称 || 'NULL'}`);
});

db.close();